package com.goalias.controller;


import com.goalias.aop.Authorized;
import com.goalias.constant.SystemConstants;
import com.goalias.feign.domain.UserInfo;
import com.goalias.resp.Result;
import com.goalias.service.INoticeService;
import com.tool.goalias.annotation.GoaliasFallback;
import com.tool.goalias.annotation.GoaliasHot;
import com.tool.goalias.enums.FlowGradeEnum;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.time.Duration;
import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/ability")
public class HelloController {

    @Autowired
    private StringRedisTemplate redisTemplate;

    @Resource
    private INoticeService noticeService;

    private final Map<String, LocalDateTime> map = new HashMap<>();

    @GetMapping("/userinfo")
    public UserInfo userinfo() {
        return UserInfo.get();
    }

    @GetMapping("/hello/{id}")
    @Authorized
    @GoaliasHot(grade = FlowGradeEnum.FLOW_GRADE_QPS, count = 100, duration = 1)
    public String hello(@PathVariable("id") Integer id) {
        redisTemplate.opsForValue().set("hello", "hello" + id, Duration.ofSeconds(60));
        return "hello" + id;
    }

    @GetMapping("/testXss")
    public String greet(@RequestParam(value = "name", defaultValue = "Guest") String name) {
        // 用户输入的内容直接显示在页面上
        return "<html><body>Hello, " + name + "!</body></html>";
    }

    @PostMapping("/testHtml")
    public Result testHtml(@RequestBody String text) {
        System.out.println(text);
        // 用户输入的内容直接显示在页面上
        String s = "<html><body>Hello, " + text + "!</body></html>";
        return Result.success(s);
    }

    @GoaliasFallback(grade = FlowGradeEnum.FLOW_GRADE_QPS, count = 5)
    @GetMapping("/test")
    public String test(@RequestParam("id") Integer id) {
//        throw new RuntimeException("test");
        return "test"+ id;
    }

    public String testFallback(Integer id) {
        String className = getClass().getName();
//        EmailDto emailDto = SystemUtil.fallbackEmailNotice(className, "test");
//        if (emailDto != null){
//        }
            noticeService.sendWarningMail(className, "test", SystemConstants.FALLBACK_NOTICE);
        return "testFall" + id;
    }
}
